<!--
  说明：面试邀约弹窗
 -->
<template>
  <!-- 新建/编辑 -->
  <el-dialog title="面试邀约" :visible.sync="dialog" width="50%" append-to-body>
    <el-form
      ref="inviteForm"
      :model="inviteForm"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item label="面试时间" prop="inviteDate">
        <el-date-picker
          v-model="inviteForm.inviteDate"
          type="datetime"
          placeholder="选择面试时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          :picker-options="pickerOptionStop"
        />
      </el-form-item>
      <el-form-item label="面试方式" prop="inviteWay">
        <el-checkbox-group v-model="inviteForm.inviteWay" style="width: 50%">
          <el-checkbox
            v-for="item in inviteWay"
            :key="item.value"
            :label="item.value"
          >{{ item.label }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="面试地点" prop="inviteAddress">
        <el-select
          v-model="inviteForm.inviteAddress"
          style="width: 50%"
          clearable
        >
          <el-option
            v-for="(item, index) in companyAddress"
            :key="index"
            :value="item.label"
            :label="item.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="其他" prop="inviteContent">
        <el-input
          v-model="inviteForm.inviteContent"
          clearable
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 8 }"
          placeholder="请填写内容"
        />
      </el-form-item>
      <el-form-item label="注意事项" prop="noticeItem">
        <el-checkbox-group v-model="inviteForm.noticeItem" style="width: 50%">
          <el-checkbox
            v-for="item in attentionEvent"
            :key="item.value"
            :label="item.value"
          >{{ item.label }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="发送短信通知" prop="isSendMsg">
        <el-checkbox-group v-model="inviteForm.isSendMsg" style="width: 50%">
          <el-checkbox :label="true">是</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <el-row type="flex" class="row-bg" justify="end">
      <el-button @click="dialog = false">取消</el-button>
      <el-button @click="submit">发送邀请</el-button>
    </el-row>
  </el-dialog>
</template>
<script>
import {
  educationOptions,
  studentType,
  workPeriod,
  inviteWay,
  attentionEvent
} from '@/utils/frontDataDict.js'
import { getCompanyByLoginUser } from '@/api/company-admin-api/hireManage/index.js'
import { deepClone } from '../../../../utils'
export default {
  name: 'InviteDialog',
  data() {
    return {
      pickerOptionStop: {
        disabledDate: (time) => {
          return time.getTime() < Date.now() - 8.64e7
        }
      },
      attentionEvent: attentionEvent, // 注意事项
      inviteWay: inviteWay, // 面试方式
      props: { multiple: true }, // 学历要求多选
      workPeriod: workPeriod, // 工作周期
      studentType: studentType, // 学生类型
      educationOptions: educationOptions, // 学历要求
      dialog: false, //  默认展示，父组件在外面控制展示隐藏
      inviteForm: {
        inviteDate: null, // 面试时间
        inviteAddress: '', // 面试地址
        inviteWay: ['0'], // 面试方式（多选），inviteWay 0 笔试，1 面试， 2 线上沟通
        inviteContent: '', // 面试描述内容，其他
        noticeItem: ['0'], // 注意事项 0 请带简历、1 请带学生证、2 请带学历证书、3 请带身份证、4 请带作品、5 请着正装
        isSendMsg: false // todo 缺少是否发送短信的字段
      },
      // 表单校验
      rules: {
        inviteDate: [
          {
            required: true,
            message: '面试日期不能为空',
            trigger: 'change'
          }
        ],
        inviteWay: [
          {
            required: true,
            type: 'array',
            message: '面试方式不能为空',
            trigger: 'change'
          }
        ],
        inviteAddress: [
          { required: true, message: '面试地点不能为空', trigger: 'change' }
        ],
        inviteContent: [
          {
            required: false,
            message: '面试内容不能为空',
            trigger: 'blur'
          }
        ],
        noticeItem: [
          {
            required: true,
            type: 'array',
            message: '注意事项不能为空',
            trigger: 'blur'
          }
        ]
      },
      companyInfo: null,
      companyAddress: [] // 企业地址数组，下拉选择
    }
  },
  methods: {
    // 获取企业介绍信息接口，拿到企业地址用来下拉选择
    getCompanyRecommentInfo() {
      return getCompanyByLoginUser().then((res) => {
        if (res.code === 0 && res.data) {
          this.companyInfo = res.data
        }
      })
    },
    async open() {
      await this.getCompanyRecommentInfo()
      this.inviteForm = Object.assign({}, this.$options.data().inviteForm)
      try {
        this.companyAddress = JSON.parse(this.companyInfo.companyAddress) || []
        this.companyAddress.map((item, index) => {
          item.label = item.province + item.city + item.address
          item.value = index
          return item
        })
      } catch (error) {
        console.error(new Error('公司地址不存在'))
      }
      this.dialog = true
    },
    close() {
      this.inviteForm = Object.assign({}, this.$options.data().inviteForm)
      this.dialog = false
      this.$emit('close')
    },
    submit() {
      this.$refs.inviteForm.validate((boo) => {
        if (boo) {
          const inviteForm = deepClone(this.inviteForm)
          inviteForm.inviteWay = JSON.stringify(inviteForm.inviteWay) // 面试方式
          inviteForm.noticeItem = JSON.stringify(inviteForm.noticeItem) // 面试注意事项
          inviteForm.isSendMsg = inviteForm.isSendMsg ? '1' : '0'
          this.$emit('submit', inviteForm)
        }
      })
    }
  }
}
</script>
